package org.tis.tools.abf.module.om.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.om.controller.request.OmBatchDeleteRequest;
import org.tis.tools.abf.module.om.controller.request.OmEmpOrgRequest;
import org.tis.tools.abf.module.om.entity.OmEmpOrg;
import org.tis.tools.abf.module.om.service.IOmEmpOrgService;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;

/**
 * omEmpOrg的Controller类
 * 员工隶属机构关系表
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/omEmpOrg")
@Validated
@Slf4j
public class OmEmpOrgController extends BaseController<OmEmpOrg>  {

    @Autowired
    private IOmEmpOrgService omEmpOrgService;


    @ApiOperation(value = "新增员工隶属机构关系",notes = "传入OmEmpOrgRequest类作为参数")
    @OperateLog(type = OperateType.ADD,desc = "新增员工隶属机构关系")
    @PostMapping
    public ResultVO add(@RequestBody @Validated @ApiParam(value = "OmEmpOrgRequest",name = "传入的OmEmpOrgRequest类",required = true) OmEmpOrgRequest omEmpOrgRequest) {
        omEmpOrgService.add(omEmpOrgRequest);
        return ResultVO.success("新增成功！");
    }


    @ApiOperation(value = "修改员工隶属机构关系",notes = "传入OmEmpOrgRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "修改员工隶属机构关系")
    @PutMapping
    public ResultVO update(@RequestBody @Validated @ApiParam(value = "OmEmpOrgRequest",name = "传入的OmEmpOrgRequest类",required = true) OmEmpOrgRequest omEmpOrgRequest) {
        OmEmpOrg omEmpOrg = omEmpOrgService.selectById(omEmpOrgRequest);
        if (omEmpOrg == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        omEmpOrgService.change(omEmpOrgRequest);
        return ResultVO.success("修改成功！");
    }


    @ApiOperation(value = "删除员工隶属机构关系",notes = "传入员工隶属机构GUID(id)作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "删除员工隶属机构关系")
    @DeleteMapping("/{id}")
    public ResultVO delete(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "员工隶属机构GUID",required = true) String id) {
        OmEmpOrg omEmpOrg = omEmpOrgService.selectById(id);
        if (omEmpOrg == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        omEmpOrgService.deleteById(id);
        return ResultVO.success("删除成功");
    }


    @ApiOperation(value = "批量删除员工隶属机构关系",notes = "传入OmBatchDeleteRequest类作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "批量删除员工隶属机构关系")
    @PostMapping("/batchDelete")
    public ResultVO batchDelete(@RequestBody @Validated @ApiParam(value = "OmBatchDeleteRequest",name = "传入的OmBatchDeleteRequest类",required = true) OmBatchDeleteRequest deleteRequest) {

        omEmpOrgService.batchDelete(deleteRequest);
        return ResultVO.success("删除成功");
    }


    @ApiOperation(value = "根据ID查询员工隶属机构关系",notes = "传入员工隶属机构关系GUID(id)作为参数")
    @GetMapping("/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "员工隶属机构GUID", required = true) String id) {
        OmEmpOrg omEmpOrg = omEmpOrgService.selectById(id);
        if (omEmpOrg == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", omEmpOrg);
    }


    @ApiOperation(value = "分页查询所有信息",notes = "传入分页信息作为参数")
    @PostMapping("/list")
    public ResultVO list(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息", required = true) SmartPage<OmEmpOrg> page) {
        return  ResultVO.success("查询成功", omEmpOrgService.selectPage(page.getPageObj(), page.getConditionWrapper()));
    }
    
}

